package com.ruoyi.flowable.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.flowable.domain.bo.WfCopyBo;
import com.ruoyi.flowable.domain.query.ProcessQuery;
import com.ruoyi.flowable.domain.vo.WfCopyVo;
import com.ruoyi.flowable.domain.vo.WfDefinitionVo;
import com.ruoyi.flowable.domain.vo.WfTaskVo;
import com.ruoyi.flowable.service.IWfCopyService;
import com.ruoyi.flowable.service.IWfProcessService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 工作流流程管理
 *
 * @author siashan
 * @createTime 2022/3/24 18:54
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/workflow/process")
public class WfProcessController extends BaseController {

    private final IWfProcessService processService;
    private final IWfCopyService copyService;

    /**
     * 查询可发起流程列表
     *
     */
    @GetMapping(value = "/list")
    @PreAuthorize("@ss.hasPermi('workflow:process:startList')")
    public TableDataInfo<List<WfDefinitionVo>> startProcessList(ProcessQuery processQuery) {
        TableDataInfo<List<WfDefinitionVo>> wfDefinitionVos = processService.selectPageStartProcessList(processQuery);
        return wfDefinitionVos;
    }

    /**
     * 我拥有的流程
     */
    @PreAuthorize("@ss.hasPermi('workflow:process:ownList')")
    @GetMapping(value = "/ownList")
    public TableDataInfo<List<WfTaskVo>> ownProcessList(ProcessQuery processQuery) {
        TableDataInfo<List<WfTaskVo>> wfTaskVos = processService.selectPageOwnProcessList(processQuery);
        return wfTaskVos;
    }

    /**
     * 获取待办列表
     */
    @PreAuthorize("@ss.hasPermi('workflow:process:todoList')")
    @GetMapping(value = "/todoList")
    public TableDataInfo<List<WfTaskVo>> todoProcessList(ProcessQuery processQuery) {
        TableDataInfo<List<WfTaskVo>> wfTaskVos = processService.selectPageTodoProcessList(processQuery);
        return wfTaskVos;
    }

    /**
     * 获取待签列表
     *
     * @param processQuery 流程业务对象
     */
    @PreAuthorize("@ss.hasPermi('workflow:process:claimList')")
    @GetMapping(value = "/claimList")
    public TableDataInfo<List<WfTaskVo>> claimProcessList(ProcessQuery processQuery) {
        TableDataInfo<List<WfTaskVo>> wfTaskVos = processService.selectPageClaimProcessList(processQuery);
        return wfTaskVos;
    }

    /**
     * 获取已办列表
     *
     */
    @PreAuthorize("@ss.hasPermi('workflow:process:finishedList')")
    @GetMapping(value = "/finishedList")
    public TableDataInfo<List<WfTaskVo>> finishedProcessList(ProcessQuery processQuery) {
        TableDataInfo<List<WfTaskVo>> wfTaskVos = processService.selectPageFinishedProcessList(processQuery);
        return wfTaskVos;
    }

    /**
     * 获取抄送列表
     *
     * @param copyBo 流程抄送对象
     */
    @PreAuthorize("@ss.hasPermi('workflow:process:copyList')")
    @GetMapping(value = "/copyList")
    public TableDataInfo copyProcessList(WfCopyBo copyBo) {
        copyBo.setUserId(getUserId());
        startPage();
        List<WfCopyVo> wfCopyVos = copyService.selectPageList(copyBo);
        return getDataTable(wfCopyVos);
    }


    /**
     * 查询流程部署关联表单信息
     *
     * @param definitionId 流程定义id
     * @param deployId 流程部署id
     */
    @GetMapping("/getProcessForm")
    @PreAuthorize("@ss.hasPermi('workflow:process:start')")
    public R<?> getForm(@RequestParam(value = "definitionId") String definitionId,
                        @RequestParam(value = "deployId") String deployId,
                        @RequestParam(value = "procInsId", required = false) String procInsId) {
        return R.ok(processService.selectFormContent(definitionId, deployId, procInsId));
    }

    /**
     * 根据流程定义id启动流程实例
     *
     * @param processDefId 流程定义id
     * @param variables 变量集合,json对象
     */
    @PreAuthorize("@ss.hasPermi('workflow:process:start')")
    @PostMapping("/start/{processDefId}")
    public AjaxResult start(@PathVariable(value = "processDefId") String processDefId, @RequestBody Map<String, Object> variables) {
        processService.startProcessByDefId(processDefId, variables);
        return AjaxResult.success("流程启动成功");

    }

    /**
     * 删除流程实例
     *
     * @param instanceIds 流程实例ID串
     */
    @DeleteMapping("/instance/{instanceIds}")
    public R<Void> delete(@PathVariable String[] instanceIds) {
        processService.deleteProcessByIds(instanceIds);
        return R.ok();
    }


    /**
     * 读取xml文件
     * @param processDefId 流程定义ID
     */
    @GetMapping("/bpmnXml/{processDefId}")
    public AjaxResult getBpmnXml(@PathVariable(value = "processDefId") String processDefId) {
        return AjaxResult.success(null, processService.queryBpmnXmlById(processDefId));
    }

    /**
     * 查询流程详情信息
     *
     * @param procInsId 流程实例ID
     * @param taskId 任务ID
     */
    @GetMapping("/detail")
    public R detail(String procInsId,  String taskId) {
        return R.ok(processService.queryProcessDetail(procInsId,  taskId));
    }
}
